/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.utils.id.IdWorker;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.contract.entity.RippleContractSymbolHierarchy;
import com.pig4cloud.pigx.contract.mongo.entity.ContractSymbolHierarchyMongo;
import com.pig4cloud.pigx.contract.service.RippleContractSymbolHierarchyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author pigx code generator
 * @date 2022-09-07 17:41:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ripplecontractsymbolhierarchy")
@Api(value = "ripplecontractsymbolhierarchy", tags = "管理")
public class RippleContractSymbolHierarchyController {

    private final RippleContractSymbolHierarchyService rippleContractSymbolHierarchyService;

    private final MongoTemplate mongo;

    private final IdWorker idWorker;

    /**
     * 分页查询
     *
     * @param rippleContractSymbolHierarchy
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getrippleContractSymbolHierarchyPage(RippleContractSymbolHierarchy rippleContractSymbolHierarchy) {
        List<RippleContractSymbolHierarchy> hierarchies = rippleContractSymbolHierarchyService.getBaseMapper().selectList(Wrappers.query(rippleContractSymbolHierarchy));
        return R.ok(hierarchies);
    }


    /**
     * 通过id查询
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(rippleContractSymbolHierarchyService.getById(id));
    }

    /**
     * 新增
     *
     * @param list
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    public R save(@RequestBody List<RippleContractSymbolHierarchy> list) {
        BaseMapper<RippleContractSymbolHierarchy> baseMapper = rippleContractSymbolHierarchyService.getBaseMapper();
        if (list.size() > 0) {
            Long symbolId = list.get(0).getSymbolId();
            mongo.remove(Query.query(Criteria.where("symbolId").is(symbolId)),
                    ContractSymbolHierarchyMongo.class);
            baseMapper.delete(new QueryWrapper<RippleContractSymbolHierarchy>() {{
                eq("symbol_id", symbolId);
            }});
        } else {
            return R.failed("层级数量不能为0");
        }
        list.forEach(e -> {
            if (e != null) {
                e.setId(idWorker.nextId());
                baseMapper.insert(e);
                ContractSymbolHierarchyMongo hierarchyMongo = new ContractSymbolHierarchyMongo();
                BeanUtils.copyProperties(e, hierarchyMongo);
                mongo.insert(hierarchyMongo);
            }
        });
        return R.ok();
    }

    /**
     * 修改
     *
     * @param rippleContractSymbolHierarchy
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    public R updateById(@RequestBody List<RippleContractSymbolHierarchy> rippleContractSymbolHierarchy) {

        return R.ok();
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Long id) {
        return R.ok(rippleContractSymbolHierarchyService.removeById(id));
    }


    /**
     * 导出excel 表格
     *
     * @param rippleContractSymbolHierarchy 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<RippleContractSymbolHierarchy> export(RippleContractSymbolHierarchy rippleContractSymbolHierarchy) {
        return rippleContractSymbolHierarchyService.list(Wrappers.query(rippleContractSymbolHierarchy));
    }

}
